package com.strive.village.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.strive.village.entity.SysMenu;
import com.strive.village.entity.SysUser;
import com.strive.village.entity.dto.SysMenuDto;
import com.strive.village.mapper.SysMenuMapper;
import com.strive.village.mapper.SysUserMapper;
import com.strive.village.service.SysMenuService;
import com.strive.village.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysUserService userService;
    @Autowired
    SysUserMapper userMapper;

    /**
     * 获取当前用户的导航菜单
     *
     * @return
     */
    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        // 获取Security中的用户信息
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser user = userService.getByUsername(username);

        // 获取菜单
        List<Long> navMenuIds = userMapper.getNavMenuIds(user.getId());
        List<SysMenu> menuList = this.listByIds(navMenuIds);
        // 转为树形结构
        List<SysMenu> menuTree = buildTreeMenu(menuList);

        // 转化为一个DTO类
        return covert(menuTree);
    }

    /**
     * 获取树形菜单列表
     *
     * @return 返回菜单树形列表
     */
    @Override
    public List<SysMenu> tree() {
        List<SysMenu> menus = this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));
        return buildTreeMenu(menus);
    }


    /**
     * 转化为前端方便识别的DTO类 数据格式
     *
     * @param menuTree
     * @return
     */
    private List<SysMenuDto> covert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtoList = new ArrayList<>();
        menuTree.forEach(menu -> {
            SysMenuDto menuDto = new SysMenuDto();
            menuDto.setId(menu.getId());
            menuDto.setName(menu.getName());
            menuDto.setComponent(menu.getComponent());
            menuDto.setPath(menu.getPath());
            menuDto.setIcon(menu.getIcon());

            // 针对子节点
            if (menu.getChildren().size() > 0) {
                menuDto.setChildren(covert(menu.getChildren()));
            }

            menuDtoList.add(menuDto);
        });

        return menuDtoList;
    }

    /**
     * 将菜单转换为一个树形结构
     *
     * @param menuList
     * @return
     */
    private List<SysMenu> buildTreeMenu(List<SysMenu> menuList) {
        List<SysMenu> resultList = new ArrayList<>();
        for (SysMenu parentMenu : menuList) {
            // 循环读取根据父节点ID添加到父节点孩子中
            for (SysMenu childrenMenu : menuList) {
                if (parentMenu.getId() == childrenMenu.getParentId()) {
                    parentMenu.getChildren().add(childrenMenu);
                }
            }
            // 提取父节点
            if (parentMenu.getParentId() == 0L) {
                resultList.add(parentMenu);
            }
        }
        return resultList;
    }
}
